package com.bibrApp.difu.actvity.login_mvp;

import android.content.Context;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;

import com.bibrApp.difu.Config;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.DialogUtils;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<ILoginView> {
    public LoginPresenter(Context context, ILoginView iView) {
        super(context, iView);
    }

    /**
     *
     * username	String	是	手机号/邮箱
     * password	String	是	登录密码
     * verifyType	String	是	验证类型
     * verifyCode	String	是	验证码
     * @param username 账户
     * @param pwd 密码
     * @param verifyType 验证类型
     * @param verifyCode 验证码
     */
    public void login(String username,String pwd,String verifyType,String verifyCode) {
        DialogUtils.showDialog(context);
        HashMap<String, Object> map = new HashMap<>();
        map.put("account", username);
        map.put("password", pwd);
        if(!TextUtils.isEmpty(verifyType)){
            map.put("verifyType", verifyType);
        }
        if(!TextUtils.isEmpty(verifyCode)){
            map.put("verifyCode",verifyCode);
        }
        Log.i("ttt","----"+map.toString());
        RetrofitNet.getInstance()
                .getApi()
                .login(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            LoginResponseBody loginRes = new Gson().fromJson(jsonStr, LoginResponseBody.class);
                            iView.success(loginRes);
                        }

                    }

                    @Override
                    protected void onSuccessOfCode(int code, String msg ,String account) {
                        super.onSuccessOfCode(code, msg ,account);
                        iView.successOfLoginCode(code,msg ,account);
                    }
                });

    }
    /**
     * 获取验证码
     *
     * @param mobile
     */
    public void getCode(String mobile) {
        DialogUtils.showDialog(context);
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("noteType", Config.BASE_URL+Config.LOGIN_URL);
        RetrofitNet.getInstance()
                .getApi()
                .getVerifyCode(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            iView.successOfCode(String.valueOf(value));
                            ToastUtils.ShowToast(context,String.valueOf(value));
                        }

                    }
                });

    }

}
